package com.wn.util;

import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @program: doctor
 * @ClassName CrawlDataWikiImpl
 * @description:
 * @author: 韩旭
 * @create: 2023-02-15 15:24
 **/
@Component
public class CrawlDataWikiImpl implements CrawlData {

    @Override
    public String getCrawlDataData(String url) throws IOException {

//        String url = "https://search.jd.com/Search?keyword=java&enc=utf-8&wq=java&pvid=b3fb38cde42140b491ea1d8fcb7e1c94";
        Connection connection = Jsoup.connect(url);
        // 伪造请求头
        connection.header("accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9");
        connection.header("accept-encoding", "gzip, deflate, br");
        connection.header("accept-language", "zh-CN,zh;q=0.9,zh-TW;q=0.8,en-US;q=0.7,en;q=0.6");
//        connection.header("referer", "https://search.jd.com/Search?keyword=linux&enc=utf-8&wq=linux&pvid=ba772e43adc143a29f4874c5237d4f1d");
        connection.header("sec-ch-ua", "\" Not A;Brand\";v=\"99\", \"Chromium\";v=\"90\", \"Google Chrome\";v=\"90\"");
        connection.header("sec-ch-ua-mobile", "?0");
        connection.header("sec-fetch-dest", "document");
        connection.header("sec-fetch-mode", "navigate");
        connection.header("sec-fetch-site", "same-origin");
        connection.header("sec-fetch-user", "?1");
        connection.header("upgrade-insecure-requests", "1");
        connection.header("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36");

        // 发起请求
        Connection.Response response = connection.ignoreContentType(true).method(Connection.Method.GET).execute();
        String body = response.body();
        // 解析网页
        Document document = Jsoup.parse(body);

        Elements elements = document.getElementsByTag("p");

        StringBuilder stringBuilder = new StringBuilder();

        for (Element element : elements) {
            Element pElement = element.select("p").first();
            String text = pElement.text();
            if(text.equals("目前暂无留言"));
            stringBuilder.append(text);
        }


        return stringBuilder.toString();
    }



}